package com.ternnetwork.batch.sms.mdao;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.ternnetwork.batch.sms.MobilePhoneShortMessage;
import com.ternnetwork.batch.sms.MobilePhoneShortMessageException;
import com.ternnetwork.batch.sms.MobilePhoneShortMessageSender;
import com.ternnetwork.commons.utils.PropUtils;


@Component("mdaoMobilePhoneShortMessageSender")
public class MdaoMobilePhoneShortMessageSender implements MobilePhoneShortMessageSender {

	private static final Log log = LogFactory.getLog(MdaoMobilePhoneShortMessageSender.class);
	
	
	public void send(MobilePhoneShortMessage sms) throws MobilePhoneShortMessageException {
		  
		   String url=PropUtils.getPropertyValue("sms.properties","sms.dmao.host");
	       SMSMessage sm = new SMSMessage();
	       /**
	        * 先设置Timestamp 取当前时间，
	        * SMSMessage 内部会自动生成 Timestamp 要求字符串
	        * 一定要在设置密码前面，密码加密用到 Timestamp
	        * 密码md5加密也是SMSMessage 内部实现
	        * 
	        */
	       sm.setTimestamp(new Date());
	       sm.setCid(PropUtils.getPropertyValue("sms.properties","sms.dmao.cid"));
	       sm.setUid(PropUtils.getPropertyValue("sms.properties","sms.dmao.uid"));
	       sm.setPwd(PropUtils.getPropertyValue("sms.properties","sms.dmao.pwd"));
	       sm.setContent(sms.getContent());
	       String[] mobilePhoneNoArray=sms.getTo().split(",");
	       sm.setMobiles(mobilePhoneNoArray);
	       ResultMessage rm= SendSMS.sendSMS(url,sm);
	   
	       log.info("手机："+sms.getTo());
	       log.info("内容："+sms.getContent());
	       log.info("返回结果："+rm.getMessage());
		
	}

	
	public void send(MobilePhoneShortMessage[] smsArray) throws MobilePhoneShortMessageException {
		
		for(MobilePhoneShortMessage sms:smsArray){
			send(sms);
		}
	}

}
